package kr.hyosang.android.penfo.adapter;

import kr.hyosang.android.penfo.R;
import kr.hyosang.android.penfo.SharedObject;
import kr.hyosang.android.penfo.data.Article;
import kr.hyosang.android.penfo.data.MainPageData;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

public class MainPageListAdapter extends BaseAdapter implements ListAdapter
{
    private MainPageData mData = null;
    
    private int headerHeight = 0;
    private int itemHeight = 0;
    private int dividerHeight = 0;
    
    
    public void setData(MainPageData data) {
        mData = data;
        mData.rearrange();
        
        float dp = SharedObject.getInstance().mAppContext.getResources().getDisplayMetrics().density;
        headerHeight = (int)(dp * 20f);
        itemHeight = (int)(dp * 40f);
        dividerHeight = (int)(dp * 1f);
    }
    
    public int getTotalHeight() {
        int nHeight = 0;
        
        for(int i=0;i<mData.allList.size();i++) {
            if(mData.allList.get(i) == null) {
                nHeight += headerHeight;
            }else {
                nHeight += itemHeight;
            }
            nHeight += dividerHeight;
            nHeight += 1;
        }
        
        return nHeight;
    }
    
    @Override
    public int getCount()
    {
        if(mData != null) {
            return mData.allList.size();
        }
        return 0;
            
    }

    @Override
    public Object getItem(int arg0)
    {
        return mData.allList.get(arg0);
    }

    @Override
    public long getItemId(int arg0)
    {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getItemViewType(int arg0)
    {
        // TODO Auto-generated method stub
        return 0;
    } 

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2)
    {
        // TODO Auto-generated method stub
        View v = arg1;
        Context context = SharedObject.getInstance().mAppContext;
        Article article = mData.getArticle(arg0);
        ViewHolder viewHolder;

        if(v == null) {
            LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = li.inflate(R.layout.main_article_item, null);
            
            viewHolder = new ViewHolder();
            viewHolder.headerTitle = (TextView)v.findViewById(R.id.mainListHeader);
            viewHolder.itemView = (LinearLayout)v.findViewById(R.id.mainListItem);
            viewHolder.subject = (TextView)v.findViewById(R.id.articleTitle);
            viewHolder.date = (TextView)v.findViewById(R.id.mainArticleDate);
            viewHolder.comment = (TextView)v.findViewById(R.id.mainArticleComment);
            
            v.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)v.getTag();
        }
        
        if(article == null) {
            //Header position
            String headerTitle;
            
            switch(mData.getHeaderType(arg0)) {
            case MainPageData.LIST_FREEBOARD: headerTitle = "Freeboard"; break;
            case MainPageData.LIST_HOTNOW: headerTitle = "Hot Now"; break;
            case MainPageData.LIST_QNA: headerTitle = "Q & A"; break;
            case MainPageData.LIST_REVIEW: headerTitle = "Review"; break;
            default: headerTitle = "Unknown";
            }
            viewHolder.headerTitle.setText(headerTitle);
            viewHolder.headerTitle.setVisibility(View.VISIBLE);
            viewHolder.itemView.setVisibility(View.GONE);
        }else {
            if(article.nBoardType == Article.BOARD_HOTNOW) {    //Hot now는 댓글 표시 안됨
                viewHolder.comment.setVisibility(View.GONE);
            }else {
                viewHolder.comment.setVisibility(View.VISIBLE);
                viewHolder.comment.setText(article.nComment + "개의 댓글");
            }
            viewHolder.subject.setText(article.subject);
            viewHolder.date.setText(article.date);
            viewHolder.headerTitle.setVisibility(View.GONE);
            viewHolder.itemView.setVisibility(View.VISIBLE);
        }
        
        return v;
    }

    @Override
    public int getViewTypeCount()
    {
        // TODO Auto-generated method stub
        return 1;
    }

    @Override
    public boolean hasStableIds()
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isEmpty()
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver arg0)
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver arg0)
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    public boolean areAllItemsEnabled()
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isEnabled(int arg0)
    {
        return (mData.getArticle(arg0) == null) ? false : true;
    }
    
    class ViewHolder {
        public int nViewType;
        public LinearLayout itemView;
        public TextView headerTitle;
        public TextView subject;
        public TextView date;
        public TextView comment;
    }

}
